home *** CD-ROM | disk | FTP | other *** search
/ Computer Shopper 125 / Computer Shopper CD-ROM Issue 125 (1998-07)(Dennis Publishing).iso / Business / Dazzler / DAZZLER.Z / CRtfParser.class (.txt) < prev    next >
Encoding:
Java Class File  |  1997-11-26  |  5.9 KB  |  382 lines

  1. import java.awt.Color;
  2. import java.awt.Font;
  3. import java.awt.Toolkit;
  4.  
  5. class CRtfParser {
  6.    private int m_nStringIndex = 0;
  7.    private int m_nRtfStringLength;
  8.    private char[] m_strRTF;
  9.    private int m_nGroup;
  10.    private int m_nFontTableGroup;
  11.    private int m_nColourTableGroup;
  12.    private CColorArray m_ColourTable;
  13.    private CStringArray m_FontTable;
  14.    private boolean m_bUnderline;
  15.    private boolean m_bStrike;
  16.    private boolean m_bProtected;
  17.    private String m_strCurrentFontName;
  18.    private int m_nCurrentSize;
  19.    private int m_nCurrentStyle;
  20.    private int m_nJustification;
  21.    private int m_nDefaultTab;
  22.    private Color m_rgbCurrent;
  23.    private RichCharArray m_RichCharArray;
  24.    private CRtfKeyword[] m_KeywordList = new CRtfKeyword[]{new CRtfKeyword("plain", 0), new CRtfKeyword("b", 1), new CRtfKeyword("i", 2), new CRtfKeyword("ul", 3), new CRtfKeyword("strike", 4), new CRtfKeyword("par", 5), new CRtfKeyword("pard", 6), new CRtfKeyword("tab", 7), new CRtfKeyword("colortbl", 8), new CRtfKeyword("cf", 9), new CRtfKeyword("red", 10), new CRtfKeyword("green", 11), new CRtfKeyword("blue", 12), new CRtfKeyword("fonttbl", 13), new CRtfKeyword("f", 14), new CRtfKeyword("fs", 15), new CRtfKeyword("fswiss", 16), new CRtfKeyword("froman", 17), new CRtfKeyword("fmodern", 18), new CRtfKeyword("fscript", 19), new CRtfKeyword("fnil", 20), new CRtfKeyword("qc", 21), new CRtfKeyword("qr", 22), new CRtfKeyword("pntxtb", 23), new CRtfKeyword("'", 24), new CRtfKeyword("deftab", 25), new CRtfKeyword("protect", 26)};
  25.  
  26.    private void ChangeFontSize(int var1) {
  27.       this.m_nCurrentSize = (int)((double)var1 * 1.35);
  28.    }
  29.  
  30.    private int ParseColourValue() {
  31.       String var1 = "";
  32.  
  33.       for(char var3 = this.GetChar(); var3 != 0 && var3 != ';' && var3 != '\\'; var3 = this.GetChar()) {
  34.          if (Character.isDigit(var3)) {
  35.             var1 = var1 + var3;
  36.          }
  37.       }
  38.  
  39.       int var2;
  40.       if (var1.length() > 0) {
  41.          var2 = Integer.valueOf(var1);
  42.       } else {
  43.          var2 = 0;
  44.       }
  45.  
  46.       return var2;
  47.    }
  48.  
  49.    private void ChangeFont(int var1) {
  50.       try {
  51.          this.m_strCurrentFontName = this.m_FontTable.GetAt(var1);
  52.       } catch (ArrayIndexOutOfBoundsException var2) {
  53.          System.out.println("Unable to retrieve rtf font table entry " + var1);
  54.       }
  55.    }
  56.  
  57.    private void ParseRtfKeyword() {
  58.       String var2 = "";
  59.       String var3 = "";
  60.       int var4 = 0;
  61.       boolean var5 = false;
  62.  
  63.       while(!var5) {
  64.          char var1 = this.GetChar();
  65.          switch (var1) {
  66.             case '\u0000':
  67.                System.out.println("Unexpected end of rtf file");
  68.             case ' ':
  69.             case '*':
  70.                var5 = true;
  71.                break;
  72.             case '\'':
  73.                var2 = "'";
  74.                var3 = new String(this.m_strRTF, this.m_nStringIndex, 2);
  75.                var4 = Integer.valueOf(var3, 16);
  76.                this.m_nStringIndex += 2;
  77.                var5 = true;
  78.                break;
  79.             case '\\':
  80.             case '{':
  81.             case '}':
  82.                if (var2.length() == 0) {
  83.                   this.ParseChar(var1);
  84.                } else {
  85.                   this.m_nStringIndex += -1;
  86.                }
  87.  
  88.                var5 = true;
  89.                break;
  90.             default:
  91.                if (!Character.isLetter(var1) && var1 != '-') {
  92.                   if (Character.isDigit(var1)) {
  93.                      var3 = var3 + var1;
  94.                   } else {
  95.                      System.out.println("Unknown character in Rtf keyword: " + var1);
  96.                   }
  97.                } else {
  98.                   var2 = var2 + var1;
  99.                }
  100.          }
  101.       }
  102.  
  103.       if (var3.length() > 0 && !var2.equals("'")) {
  104.          var4 = Integer.valueOf(var3);
  105.       }
  106.  
  107.       this.TranslateKeyword(var2, var4);
  108.    }
  109.  
  110.    RichCharArray ParseRtfFile(String var1) {
  111.       if (this.m_nRtfStringLength > 0) {
  112.          while(true) {
  113.             char var2;
  114.             if ((var2 = this.GetChar()) != 0) {
  115.                switch (var2) {
  116.                   case '\n':
  117.                   case '\r':
  118.                      break;
  119.                   case '\\':
  120.                      this.ParseRtfKeyword();
  121.                      break;
  122.                   case '{':
  123.                      ++this.m_nGroup;
  124.                      break;
  125.                   case '}':
  126.                      if (this.m_nFontTableGroup == this.m_nGroup) {
  127.                         this.m_nFontTableGroup = -1;
  128.                      }
  129.  
  130.                      if (this.m_nColourTableGroup == this.m_nGroup) {
  131.                         this.m_nColourTableGroup = -1;
  132.                      }
  133.  
  134.                      this.m_nGroup += -1;
  135.                      break;
  136.                   default:
  137.                      if (this.m_nFontTableGroup == -1 && this.m_nColourTableGroup == -1) {
  138.                         this.ParseChar(var2);
  139.                      }
  140.                }
  141.  
  142.                if (this.m_nGroup >= 0) {
  143.                   continue;
  144.                }
  145.  
  146.                System.out.println("Too many close braces in RTF action: Parsing aborted");
  147.             }
  148.  
  149.             if (this.m_nGroup > 0) {
  150.                System.out.println("Too few close braces in RTF action: Parsing OK, check RTF file");
  151.             }
  152.             break;
  153.          }
  154.       }
  155.  
  156.       return this.m_RichCharArray;
  157.    }
  158.  
  159.    private String ParseFontKeyword(String var1) {
  160.       String var3 = "";
  161.       boolean var4 = false;
  162.  
  163.       while(!var4) {
  164.          char var2 = this.GetChar();
  165.          switch (var2) {
  166.             case '\u0000':
  167.                System.out.println("Unexpected end of rtf file");
  168.             case ' ':
  169.             case ';':
  170.                var4 = true;
  171.                break;
  172.             case '\\':
  173.                this.m_nStringIndex += -1;
  174.                var4 = true;
  175.                break;
  176.             default:
  177.                var3 = var3 + var2;
  178.          }
  179.       }
  180.  
  181.       int var5 = 0;
  182.  
  183.       do {
  184.          if (this.m_KeywordList[var5].strKeyword.equals(var3)) {
  185.             switch (this.m_KeywordList[var5].nKeyword) {
  186.                case 16:
  187.                case 19:
  188.                case 20:
  189.                   return "Helvetica";
  190.                case 17:
  191.                   return "TimesRoman";
  192.                case 18:
  193.                   return "Courier";
  194.             }
  195.          }
  196.  
  197.          ++var5;
  198.       } while(var5 < 27);
  199.  
  200.       return var1;
  201.    }
  202.  
  203.    private void ParseChar(char var1) {
  204.       Font var3 = new Font(this.m_strCurrentFontName, this.m_nCurrentStyle, this.m_nCurrentSize);
  205.       int var4 = 0;
  206.       if (this.m_nJustification == 1) {
  207.          var4 |= 1;
  208.       } else if (this.m_nJustification == 2) {
  209.          var4 |= 2;
  210.       }
  211.  
  212.       if (this.m_bProtected) {
  213.          var4 |= 4;
  214.       }
  215.  
  216.       if (this.m_bUnderline) {
  217.          var4 |= 8;
  218.       }
  219.  
  220.       if (this.m_bStrike) {
  221.          var4 |= 16;
  222.       }
  223.  
  224.       RichChar var2;
  225.       if (var1 == '\t') {
  226.          var2 = new RichChar(var1, var3, this.m_rgbCurrent, this.m_nDefaultTab, var4);
  227.       } else {
  228.          var2 = new RichChar(var1, var3, this.m_rgbCurrent, Toolkit.getDefaultToolkit().getFontMetrics(var3).charWidth(var1), var4);
  229.       }
  230.  
  231.       this.m_RichCharArray.Add(var2);
  232.    }
  233.  
  234.    private void ParseFontTableEntry(int var1) {
  235.       String var2 = "";
  236.       String var3 = "";
  237.  
  238.       for(char var4 = this.GetChar(); var4 != 0 && var4 != ';'; var4 = this.GetChar()) {
  239.          if (var4 == '\\') {
  240.             var3 = this.ParseFontKeyword(var3);
  241.          } else {
  242.             var2 = var2 + var4;
  243.          }
  244.       }
  245.  
  246.       if (var3.equals("")) {
  247.          var2 = Utils.MapFont(var2);
  248.          this.m_FontTable.SetAtGrow(var1, var2);
  249.       } else {
  250.          this.m_FontTable.SetAtGrow(var1, var3);
  251.       }
  252.    }
  253.  
  254.    private void ChangeFontStyle(int var1) {
  255.       if (var1 == 0) {
  256.          this.m_nCurrentStyle = 0;
  257.          this.m_rgbCurrent = Color.black;
  258.          this.m_bUnderline = false;
  259.          this.m_bStrike = false;
  260.          this.m_bProtected = false;
  261.       } else {
  262.          this.m_nCurrentStyle += var1;
  263.       }
  264.    }
  265.  
  266.    private char GetChar() {
  267.       char var1 = 0;
  268.       if (this.m_nStringIndex < this.m_nRtfStringLength) {
  269.          var1 = this.m_strRTF[this.m_nStringIndex++];
  270.       }
  271.  
  272.       return var1;
  273.    }
  274.  
  275.    private void ChangeColour(int var1) {
  276.       try {
  277.          this.m_rgbCurrent = this.m_ColourTable.GetAt(var1);
  278.       } catch (ArrayIndexOutOfBoundsException var2) {
  279.          System.out.println("Unable to retrieve rtf colour table entry " + var1);
  280.       }
  281.    }
  282.  
  283.    CRtfParser(String var1) {
  284.       this.m_nRtfStringLength = var1.length();
  285.       this.m_strRTF = new char[this.m_nRtfStringLength];
  286.       this.m_strRTF = var1.toCharArray();
  287.       this.m_nGroup = 0;
  288.       this.m_nFontTableGroup = -1;
  289.       this.m_nColourTableGroup = -1;
  290.       this.m_ColourTable = new CColorArray();
  291.       this.m_FontTable = new CStringArray();
  292.       this.m_RichCharArray = new RichCharArray();
  293.       this.m_bUnderline = false;
  294.       this.m_bStrike = false;
  295.       this.m_bProtected = false;
  296.    }
  297.  
  298.    private void ParseColourTableEntry(int var1) {
  299.       int var2 = this.ParseColourValue();
  300.       int var3 = this.ParseColourValue();
  301.       Color var4 = new Color(var1, var2, var3);
  302.       this.m_ColourTable.Add(var4);
  303.    }
  304.  
  305.    private void TranslateKeyword(String var1, int var2) {
  306.       int var3 = 0;
  307.  
  308.       do {
  309.          if (this.m_KeywordList[var3].strKeyword.equals(var1)) {
  310.             switch (this.m_KeywordList[var3].nKeyword) {
  311.                case 0:
  312.                   this.ChangeFontStyle(0);
  313.                   break;
  314.                case 1:
  315.                   this.ChangeFontStyle(1);
  316.                   break;
  317.                case 2:
  318.                   this.ChangeFontStyle(2);
  319.                   break;
  320.                case 3:
  321.                   this.m_bUnderline = true;
  322.                   break;
  323.                case 4:
  324.                   this.m_bStrike = true;
  325.                case 5:
  326.                   this.ParseChar('\n');
  327.                   break;
  328.                case 6:
  329.                   this.m_nJustification = 0;
  330.                   break;
  331.                case 7:
  332.                   this.ParseChar('\t');
  333.                   break;
  334.                case 8:
  335.                   this.m_nColourTableGroup = this.m_nGroup;
  336.                   break;
  337.                case 9:
  338.                   this.ChangeColour(var2);
  339.                   break;
  340.                case 10:
  341.                   ++this.m_nStringIndex;
  342.                   this.ParseColourTableEntry(var2);
  343.                   break;
  344.                case 13:
  345.                   this.m_nFontTableGroup = this.m_nGroup;
  346.                   break;
  347.                case 14:
  348.                   if (this.m_nFontTableGroup == -1) {
  349.                      this.ChangeFont(var2);
  350.                   } else {
  351.                      this.ParseFontTableEntry(var2);
  352.                   }
  353.                   break;
  354.                case 15:
  355.                   this.ChangeFontSize(var2 / 2);
  356.                   break;
  357.                case 21:
  358.                   this.m_nJustification = 2;
  359.                   break;
  360.                case 22:
  361.                   this.m_nJustification = 1;
  362.                   break;
  363.                case 23:
  364.                   this.m_nStringIndex += 4;
  365.                   break;
  366.                case 24:
  367.                   this.ParseChar((char)var2);
  368.                   break;
  369.                case 25:
  370.                   this.m_nDefaultTab = var2 / 15;
  371.                   break;
  372.                case 26:
  373.                   this.m_bProtected = true;
  374.             }
  375.          }
  376.  
  377.          ++var3;
  378.       } while(var3 < 27);
  379.  
  380.    }
  381. }
  382.